//
//  ShopFilterView.m
//  sensen_ios
//
//  Created by pro on 16/2/29.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopFilterView.h"

@implementation ShopFilterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    
    CGFloat x, y, width, height;
    
    // 标题
    x = 15;
    y = 0;
    width = 0;
    height = self.height / 2;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.userInteractionEnabled = YES;
    titleLabel.font = FONT(15);
    titleLabel.text = @"价格区间(元)";
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:titleLabel];
    titleLabel.width = [titleLabel.text getSizeOfString:15].width;
    
    // 两个输入框
    x = titleLabel.right + 10;
    height = 25;
    width = (self.width - x - 10 - 30) / 2;
    y = (self.height / 2 - height) / 2;
    UITextField *lowPriceTf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    lowPriceTf.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    lowPriceTf.textColor = [UIColor darkGrayColor];
    lowPriceTf.textAlignment = NSTextAlignmentCenter;
    lowPriceTf.text = @"";
    lowPriceTf.keyboardType = UIKeyboardTypeNumberPad;
    lowPriceTf.layer.cornerRadius = 4;
    [self addSubview:lowPriceTf];
    lowPriceTf.tag = 1;
    
    // 线
    x = lowPriceTf.right + 5;
    width = 20;
    height = 1;
    y = (self.height / 2 - height) / 2;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1];
    [self addSubview:line];

    x = line.right + 5;
    height = 25;
    width = lowPriceTf.width;
    y = (self.height / 2 - height) / 2;
    UITextField *highPriceTf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    highPriceTf.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    highPriceTf.textColor = [UIColor darkGrayColor];
    highPriceTf.textAlignment = NSTextAlignmentCenter;
    highPriceTf.text = @"";
    highPriceTf.keyboardType = UIKeyboardTypeNumberPad;
    highPriceTf.layer.cornerRadius = 4;
    [self addSubview:highPriceTf];
    highPriceTf.tag = 2;
    
    // 重置按钮和确认按钮
    NSArray *titles = @[@"重置",@"确认"];
    NSArray *colors = @[GREEN_COLOR,SS_NAVI_ORANGE_LIGHT_COLOR];
    CGFloat margin = 20;
    width = (self.width / 2 - margin * 3) / 2;
    height = 30;
    y = titleLabel.bottom;
    for (int i = 0; i < titles.count; i ++) {
        x = self.width / 2 + margin + (width + margin) * i;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, width, height);
        button.backgroundColor = colors[i];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 5;
        [self addSubview:button];
        button.tag = 10 + i;
    }
}



#pragma mark - 点击事件
- (void)buttonAction:(UIButton *)button {
    UITextField *tf1 = (UITextField *)[self viewWithTag:1];
    UITextField *tf2 = (UITextField *)[self viewWithTag:2];
    switch (button.tag) {
        case 10: // 重置
        {
            tf1.text = @"";
            tf2.text = @"";
        }
            break;
        case 11: // 确认
        {
            NSString *filterRange;
            // 有一个未输入，或都没输入，，或输入有负数，则返回nil
            if ([tf1.text isEqualToString:@""] || [tf2.text isEqualToString:@""] || [tf1.text integerValue] < 0 || [tf2.text integerValue] < 0) {
                filterRange = nil;
            } else {
                filterRange = [NSString stringWithFormat:@"%@-%@",tf1.text, tf2.text];
            }
            
            [self endEditing:YES];
            // 执行代理
            if ([self.delegate respondsToSelector:@selector(filterViewDidChooseRange:)]) {
                [self.delegate filterViewDidChooseRange:filterRange];
            }
            
            tf1.text = @"";
            tf2.text = @"";
        }
            break;
            
        default:
            break;
    }
}
@end
